/**
 * Created by DELL on 2018/9/7.
 */
(function (ns) {

  var Key = ns.Key = Hilo.Class.create({
    Extends: Hilo.Sprite,
    constructor: function (properties) {
      Key.superclass.constructor.call(this, properties);

      this.addFrame(properties.atlas.getSprite('bird'));
      //初始化障碍的宽和高
      // this.width = 180;
      // this.height = 60;
      this.interval = 6;
      this.pivotX = 43;
      this.pivotY = 30;
      this.x = properties.startX;
      this.y = 0;
      this.hasClick = false;  // 是否被点击过
      this.stageHeight = properties.height;

      this.moveTween = new Hilo.Tween(this, null, {
        onComplete: this.moveEnd.bind(this)
      });
      this.on(Hilo.event.POINTER_START, this.onUserclick.bind(this));
      this.startMove();

    },
    startMove: function () {
      //设置缓动时间
      this.moveTween.duration = 2000;
      //设置缓动的变换属性，即x从当前坐标变换到targetX
      this.moveTween.setProps({y: 0}, {y: this.stageHeight});
      //启动缓动动画
      this.moveTween.start();
      // this.tween = Hilo.Tween.to(this, {y: this.stageHeight, rotation: -8}, {duration: 1000});
    },
    // 点击事件
    onUserclick: function () {
      if (!this.hasClick) {
        ns.updataScore();
        this.hasClick = true;
        this.moveTween.stop();
        this.alpha = 0.5;

        /*一闪一闪*/
        this.moveTween.duration = 300;
        this.moveTween.reverse = true;
        this.moveTween.repeat = 3;
        this.moveTween.onComplete = this.birdDied.bind(this);
        this.moveTween.setProps({alpha: 0.3}, {alpha: 0.9});
        this.moveTween.start();
      }
    },
    birdDied: function () {
      console.log('消失');
      this.removeFromParent();
    },
    moveEnd: function () {
      console.log('移动完毕');
      this.removeFromParent();
    }

  });

})(window.game);